package com.wits.qso.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonBeanProcessor;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.springframework.format.annotation.DateTimeFormat;

import com.wits.platform.entity.BaseEntity;
import com.wits.platform.entity.User;

@Entity
@Table(name = "qso_vm_user")
public class VmUser extends BaseEntity
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6099224285717803932L;
	
	private String id = null;
	private User user = null;
	private Date dateF = null;
	private Date dateT = null;
	private Vm vm = null;

	public VmUser() { super(); }

	@Id
	@GenericGenerator(name = "idGen", 
	strategy = "com.wits.platform.common.id.generator.IDGenerator",
	parameters={ 
				@Parameter(name = "segment_value", value = "QSO_VM_USER_ID"), 
				@Parameter(name="table_name",value="wits_id_generator"),
				@Parameter(name="value_column_name",value="gen_value"),
				@Parameter(name="segment_column_name",value="gen_name"),
				@Parameter(name="segment_value_length",value="5"),
				@Parameter(name="sq_format",value="%03d"), 
				@Parameter(name="sq_prefix",value="CL") 
	})
	@GeneratedValue(generator = "idGen")
	@Column(name = "vm_user_id", nullable = false, insertable = true, updatable = false, length = 50)
	public String getId() {
		return id;
	}

	@ManyToOne()
	@JoinColumn(name = "user_id")
	public User getUser() {
		return user;
	}

	@Temporal(TemporalType.DATE)
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	@Column(name = "d_open_f")
	public Date getDateF() {
		return dateF;
	}

	@Temporal(TemporalType.DATE)
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	@Column(name = "d_open_t")
	public Date getDateT() {
		return dateT;
	}

	@ManyToOne()
	@JoinColumn(name = "vm_id")
	public Vm getVm() {
		return vm;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setDateF(Date dateF) {
		this.dateF = dateF;
	}

	public void setDateT(Date dateT) {
		this.dateT = dateT;
	}

	public void setVm(Vm vm) {
		this.vm = vm;
	}

	@Override
	public String display() {
		return null;
	}

	public String toString(){
		return "{\"id\":\"" + this.id 
				+ "\",\"userName\":\"" + this.user.getUserNameC()
				+ "\",\"dateF\":\"" + this.dateF 
				+ "\",\"dateT\":\""+ this.dateT
				+ "\",\"ip\":\""+ this.vm.getIp()
				+ "\"}";
	}
	
	private static JsonConfig jsonConfig = new JsonConfig();

	@Transient
	public static JsonConfig getJsonConfig() {
		return jsonConfig;
	}

	static{
		jsonConfig.registerJsonBeanProcessor(VmUser.class, new JsonBeanProcessor(){
			@Override
			public JSONObject processBean(Object bean, JsonConfig jConf) {
				if(!(bean instanceof VmUser)){
		            return new JSONObject(true);
		         }
				VmUser vu = (VmUser) bean;
				Vm v=vu.getVm();
				User u=vu.getUser();
		         return new JSONObject()
		            .element("userName",u!=null?u.getUserNameC():"")
		            .element("vmId",v!=null?v.getId():"");
		      }
			});
		
		
	}
}
